﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class BorderLabel : Label
    {
        private int gap;
        private bool highlight;
        private Pen hoverPen;
        private Pen pen;
        private int width;

        public BorderLabel(Color BorderColor) : this(BorderColor, 1)
        {
        }

        public BorderLabel(Color BorderColor, Color HoverColor) : this(BorderColor, HoverColor, 1)
        {
        }

        public BorderLabel(Color BorderColor, int Width)
        {
            this.pen = null;
            this.hoverPen = null;
            this.highlight = false;
            this.width = -1;
            this.gap = 0;
            this.highlight = false;
            this.width = Width;
            this.pen = new Pen(BorderColor, (float) Width);
            this.pen.Alignment = PenAlignment.Inset;
            if (Width == 1)
            {
                this.gap = 1;
            }
        }

        public BorderLabel(Color BorderColor, Color HoverColor, int Width)
        {
            this.pen = null;
            this.hoverPen = null;
            this.highlight = false;
            this.width = -1;
            this.gap = 0;
            this.highlight = true;
            this.width = Width;
            this.pen = new Pen(BorderColor, (float) Width);
            this.hoverPen = new Pen(HoverColor, (float) Width);
            this.pen.Alignment = PenAlignment.Inset;
            this.hoverPen.Alignment = PenAlignment.Inset;
            if (Width == 1)
            {
                this.gap = 1;
            }
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (this.highlight)
            {
                Graphics graphics = base.CreateGraphics();
                Rectangle bounds = base.Bounds;
                graphics.DrawRectangle(this.pen, 0, 0, bounds.Width - this.gap, bounds.Height - this.gap);
                graphics.Dispose();
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (this.highlight)
            {
                Graphics graphics = base.CreateGraphics();
                Rectangle bounds = base.Bounds;
                graphics.DrawRectangle(this.hoverPen, 0, 0, bounds.Width - this.gap, bounds.Height - this.gap);
                graphics.Dispose();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Rectangle bounds = base.Bounds;
            e.Graphics.DrawRectangle(this.pen, 0, 0, bounds.Width - this.gap, bounds.Height - this.gap);
        }
    }
}

